package com.sunchangming.rpcgen;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

import com.thoughtworks.xstream.annotations.XStreamAlias;

import freemarker.template.Template;
import freemarker.template.TemplateException;

@XStreamAlias("enum")
public class Enum extends UserType {
	private final java.util.ArrayList<EnumElement> eles = new java.util.ArrayList<EnumElement>();

	void add(EnumElement e) {
		eles.add(e);
	}

	public void generateCode(Path src, String language) throws IOException, TemplateException {
		final Path outputFile = parent.getDir(src).resolve(name + ".java");
		final freemarker.template.Configuration cfg = Main.getFreeMarkerConfig();
		final Template tmpl = cfg.getTemplate("enum.tmpl");
		final Map<Object, Object> root = new HashMap<Object, Object>();
		root.put("packagename", parent.getFullName());
		root.put("classname", name);
		root.put("eles", eles);
		tmpl.process(root, new OutputStreamWriter(Files.newOutputStream(outputFile), "utf-8"));
	}

}
